﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaxAssistantBase;

namespace ShiBieProject
{
    class InvoiceDetaileName
    {
        public static void VerificationDetaileName(List<InvoiiceOcrModel> bpmlist) {
            List<BatchInfo> productNameList = new List<BatchInfo>();
            List<BatchInfo> productNameContrastList = new List<BatchInfo>();
            //按照字段长度从大到小排序
            var listsort = from gf in HongFengCompanyOrVendorsTool.ProductNameAll orderby gf.productName.Length  descending select gf;
            var listcontrastsort = from gf in HongFengCompanyOrVendorsTool.ProductNameContrastAll orderby gf.ocrProductName.Length  descending select gf;
            int tiaonum = 0;
            foreach (InvoiiceOcrModel im in bpmlist) {
                tiaonum++;
                try
                {
                    if (im.billtype != -1)
                    {
                        if (im.invoiceOcrDetailedDtoList != null && im.invoiceOcrDetailedDtoList.Count > 0)
                        {
                            foreach (invoiceOcrDetailedModel iodm in im.invoiceOcrDetailedDtoList)
                            {
                                setDetailInitialValue(iodm);
                                var name = iodm.productName;
                                var namelist = from gf in HongFengCompanyOrVendorsTool.ProductNameAll where gf.productName == name select gf;
                                var nameContrastlist = from gf in HongFengCompanyOrVendorsTool.ProductNameContrastAll where gf.ocrProductName == name select gf;
                                productNameList = namelist.ToList();
                                productNameContrastList = nameContrastlist.ToList();
                                if (productNameList == null)
                                {
                                    productNameList = new List<BatchInfo>();
                                }
                                if (productNameContrastList == null)
                                {
                                    productNameContrastList = new List<BatchInfo>();
                                }
                                if (productNameList.Count == 0)
                                {
                                    if (productNameContrastList.Count == 0)
                                    {
                                        int tempVendbiaoshi = 0;
                                        int tempprobiaoshi = 0;
                                        //原表进行模糊匹配
                                        foreach (BatchInfo bifproduct in listsort.ToList())
                                        {
                                            string prodictname = bifproduct.productName;
                                            if (name.Length > prodictname.Length)
                                            {
                                                if (name.Contains(prodictname))
                                                {
                                                    tempVendbiaoshi++;
                                                    iodm.productName = prodictname;
                                                    break;
                                                }
                                            }
                                        }
                                        if (tempVendbiaoshi == 0)
                                        {
                                            //对照表模糊匹配
                                            foreach (BatchInfo bifproduct in listcontrastsort.ToList())
                                            {
                                                string ocrProductName = bifproduct.ocrProductName;
                                                if (name.Length > ocrProductName.Length)
                                                {
                                                    if (name.Contains(ocrProductName))
                                                    {
                                                        tempprobiaoshi++;
                                                        iodm.productName = bifproduct.productName;
                                                        break;
                                                    }
                                                }
                                            }
                                            if (tempprobiaoshi == 0)
                                            {
                                                iodm.productName = "";
                                            }
                                        }
                                    }
                                    else
                                    {
                                        iodm.productName = productNameContrastList[0].productName;
                                        iodm.productNameState = CompanyOrMoneyStateEnum.InvoiceDetaileNamEditRight;//明细内容修正正确
                                    }

                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WsdLogger.yxlog("error", "第" + tiaonum + "条核验明细内容在明细内容库中是否存在错误" + ex.Message);
                    continue;
                }
            
            }
        }

        private static void setDetailInitialValue(invoiceOcrDetailedModel iodm)
        {
            iodm.productNameInitialValue = iodm.productName;
            iodm.moneyAmountInitialValue = iodm.moneyAmount;
            iodm.detaileTaxRateInitialValue = iodm.detaileTaxRate;
            iodm.taxAmountInitialValue = iodm.taxAmount;
        }
    }
}
